import type { WindData } from 'cesium-wind-layer'

export default function transformData(gribData: GribData[]): Promise<WindData> {
  let uComp: GribData
  let vComp: GribData
  return new Promise((resolve, reject) => {
    gribData.forEach((data) => {
      switch (data.header.parameterCategory + ',' + data.header.parameterNumber) {
        case '1,2':
        case '2,2':
          uComp = data
          break
        case '1,3':
        case '2,3':
          vComp = data
          break
      }
    })
    if (!vComp || !uComp) {
      reject('缺少 U 和 V 向量')
    } else {
      resolve({
        u: {
          array: uComp.data,
          min: Math.min(...uComp.data),
          max: Math.max(...uComp.data)
        },
        v: {
          array: vComp.data,
          min: Math.min(...vComp.data),
          max: Math.max(...vComp.data)
        },
        width: uComp.header.nx,
        height: uComp.header.ny,
        bounds: {
          west: uComp.header.lo1,
          south: uComp.header.la2,
          east: uComp.header.lo2,
          north: uComp.header.la1
        }
      })
    }
  })
}
